package com.hxy.manager.activity.goods;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.hxy.manager.R;
import com.hxy.manager.entity.GoodsEntity;
import com.hxy.manager.service.GoodsEntityService;
import com.hxy.manager.util.StringUtil;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by 傲寒宏志 on 2018/1/1.
 */

public class GoodsUpdateActivity extends Activity {
    TextView textView;
    EditText editText1;
    EditText editText2;
    EditText editText3;
    EditText editText4;
    EditText editText5;
    EditText editText6;
    GoodsEntity user;
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.update);
        setTitle("商品修改");

        getActionBar().setDisplayHomeAsUpEnabled(true);//向上返回
        textView = (TextView) findViewById(R.id.textView);
        editText1 = (EditText) findViewById(R.id.editText1);
        editText2 = (EditText) findViewById(R.id.editText2);
        editText3 = (EditText) findViewById(R.id.editText3);
        editText4 = (EditText) findViewById(R.id.editText4);
        editText5 = (EditText) findViewById(R.id.editText5);
        editText6 = (EditText) findViewById(R.id.editDateEditText);

        Intent intent = getIntent();
        String scanCode = intent.getStringExtra("scanCode");
        Log.i("kan", "Update页面的接收：" + scanCode);

         user = GoodsEntityService.getOneByNumber(GoodsUpdateActivity.this, scanCode);

        if (user != null) {
            textView.setText(user.getNumber());
            editText1.setText(user.getName());
            editText2.setText(user.getPrice());
            editText3.setText(user.getPress());
            editText4.setText(user.getDate());
            editText5.setText(user.getAuthor());
            //  editText6.setText(user.getLocation());
            Calendar now = Calendar.getInstance();
            editText6.setText(now.get(Calendar.YEAR) + "年" + (now.get(Calendar.MONTH) + 1) + "月" + now.get(Calendar.DAY_OF_MONTH) + "日");

        } else {
            Toast.makeText(GoodsUpdateActivity.this, "数据异常", Toast.LENGTH_SHORT).show();

        }

        context = this;
        Button update = (Button) findViewById(R.id.update);
        update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (StringUtil.isEmpty(editText1.getText().toString())) {

                    Toast.makeText(GoodsUpdateActivity.this, "名称不可为空！", Toast.LENGTH_SHORT).show();

                } else if (editText2.getText().toString().equals("")) {

                    Toast.makeText(GoodsUpdateActivity.this, "价格不可为空！", Toast.LENGTH_SHORT).show();

                } else {

                    user.setNumber(textView.getText().toString());
                    user.setName(editText1.getText().toString());
                    user.setPrice(editText2.getText().toString());
                    user.setPress(editText3.getText().toString());
                    user.setDate(editText4.getText().toString());
                    user.setAuthor(editText5.getText().toString());
                    Calendar now = Calendar.getInstance();
                    user.setLocation(now.get(Calendar.YEAR) + "年" + (now.get(Calendar.MONTH) + 1) + "月" + now.get(Calendar.DAY_OF_MONTH) + "日");
                    user.setLocation(editText6.getText().toString());

                    //取当前年月日时分秒最为编码
                    Date d = new Date();
                    //格式化为一串数字
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");//yyyy/MM/dd HH:mm:ss
                    String dateNowStr = sdf.format(d);

                    user.setUpdateTime(System.currentTimeMillis());

                    GoodsEntityService.update(context, user);

                    Toast.makeText(GoodsUpdateActivity.this, "保存成功！", Toast.LENGTH_SHORT).show();
                    finish();

                }


            }
        });
        Button delete = (Button) findViewById(R.id.delete);
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                delete();
                Log.i("kan", "点击了删除数据");
            }
        });


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //  getMenuInflater().inflate(R.menu.main,menu);
        return super.onCreateOptionsMenu(menu);
    }


    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case android.R.id.home:  //默认添加的，向上返回上一层
                finish();
                break;
        }
        return false;
    }

    //删除联系人
    private void delete() {
        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setTitle("系统信息");
        alert.setMessage("是否删除？");
        alert.setPositiveButton("是", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int whichButton) {
                GoodsEntityService.delete(context, user);
                Toast.makeText(GoodsUpdateActivity.this, "删除成功！", Toast.LENGTH_LONG).show();
                finish();

            }
        });

        alert.setNegativeButton("否", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {

            }
        });
        alert.show();
    }
}
